﻿using RimWorld;
using Verse;
using System;
using Verse.AI;

namespace RimZombieGirl
{
    public class HediffComp_CurseNightmare : HediffComp
    {
        private int lastNightmareDay = -1;

        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);

            if (Find.TickManager.TicksGame % 3000 != 0)
                return;

            Pawn pawn = this.Pawn;

            // 只在睡眠时触发
            if (!pawn.RaceProps.Humanlike || pawn.Awake())
                return;

            int currentDay = GenLocalDate.DayOfYear(parent.pawn.Map);

            // 检查当天是否已经触发过
            if (lastNightmareDay == currentDay)
                return;

            // 随机触发噩梦
            if (Rand.Value > 0.2f)
                return;

            // 打断睡眠
            pawn.jobs.EndCurrentJob(JobCondition.InterruptForced);

            // 添加噩梦心情
            if (pawn.needs.mood != null)
                pawn.needs.mood.thoughts.memories.TryGainMemory(ThoughtDef.Named("RI_Nightmare"));

            // 记录今天已触发
            lastNightmareDay = currentDay;

            // 左上角提醒
            Messages.Message($"{pawn.Name}做了一个可怕的噩梦……".Translate(), pawn, MessageTypeDefOf.NeutralEvent);
        }
    }

    public class HediffCompProperties_CurseNightmare : HediffCompProperties
    {
        public HediffCompProperties_CurseNightmare()
        {
            this.compClass = typeof(HediffComp_CurseNightmare);
        }
    }
}
